<?php

namespace App\Store\Controllers\Goods\GoodsSend;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Common\Services\CommonExcelExport;
use App\Store\Models\Goods\GoodsSend\GoodsSendgoodsLog;
use Maatwebsite\Excel\Facades\Excel;
use App\Store\Services\Excel\DownloadTemp;
use Illuminate\Support\Facades\DB;

/**
 * 流水列表
 */
class GoodsSendgoodsLogController extends Controller
{
    /**
     * 列表
     */
    public function getlist(Request $request)
    {
        $list = GoodsSendgoodsLog::getList($request);
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }

    /**
     * 导出
     */
    public function doexport(Request $request)
    {
        $list = GoodsSendgoodsLog::getList($request)['list'];
        $exceldata = [];
        //1采购入库、2调拨入库、3赠送出库\4、调拨出库；5、取消入库；6、取消调拨；7、销售取消
        $type_arr = [
            '1' => '采购入库',
            '2' => '调拨入库',
            '3' => '赠送出库',
            '4' => '调拨出库',
            '5' => '取消入库',
            '6' => '取消调拨',
            '7' => '销售取消',
        ];
        $row = [
            ['start_store'=>'赠品流水列表'],
            [
                // 'store_name' => '所属门店',
                'start_store_name' => '起始门店',
                'target_store_name' => '目的门店',
                'sn_code' => "关联单号",
                'sendgoods_name' => '赠品名称',
                'goods_sn_code' => '货号',
                'count' => '库存数量',
                'type' => '类型',
                'create_at' => '创建时间',
                'remark' => '备注',
            ]
        ];
        foreach ($list as $val) {
            $exceldata[] = [
                'store_name' => $val['storeinfo'] ? $val['storeinfo']['store_name'] : '财务部',
                'start_store_name' => $val['startstoreinfo'] ? $val['startstoreinfo']['store_name'] : '财务部',
                'target_store_name' => $val['targetstoreinfo'] ? $val['targetstoreinfo']['store_name'] : '财务部',
                'sn_code' => $val['sn_code'],
                'sendgoods_name' => $val['sendgoods_name'],
                'goods_sn_code' => $val['goods_sn_code'],
                'count' => $val['count'],
                'type' => $type_arr[$val['type']],
                'create_at' => date('Y-m-d H:i:s', $val['create_at']),
                'remark' => $val['remark'],
            ];
        }

        $filename = '赠品流水信息导出_' . time() . '.xlsx';
        return Excel::download(new CommonExcelExport($row, $exceldata), $filename);
    }

}
